//
//  YGProductBLL.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/25.
//

#import "YGProductBLL.h"
#import "CNHTTPManager.h"
#import "UserInfoManager.h"
#import "YGGoodsSelectModel.h"
#import "YGProductItemModel.h"
@implementation YGProductBLL


+ (void)requestSearchWithSuccess:(void (^)(YGSearchProductModel *model))success fail:(void (^)(NSError *error))fail{
    NSString *token = [[UserInfoManager shareInstance] getUserInfo].token;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:token forKey:@"token"];
    [[CNHTTPManager shared] GET:YG_SEARCH_URL parameters:dic success:^(id responeObj) {
        NSDictionary *data = [responeObj objectForKey:@"data"];
        YGSearchProductModel *model = [[YGSearchProductModel alloc] initWithDictionary:data error:nil];
        if (success) {
            success(model);
        }
    } failure:^(NSError *error, NSString *errorDes) {
        if (fail) {
            fail(error);
        }
    }];

}

+ (void)requestProductTypeWithSuccess:(void(^)(NSArray<YGGoodsSelectModel *> *model))success fail:(void (^)(NSError *error))fail{
  
    [[CNHTTPManager shared] GET:YG_GOODS_TYPE_URL parameters:nil success:^(id responeObj) {
        NSArray *data = [responeObj objectForKey:@"data"];
        NSArray *models = [YGGoodsSelectModel arrayOfModelsFromDictionaries:data];
            if (success) {
                success(models);
            }
        } failure:^(NSError *error, NSString *errorDes) {
            if (fail) {
                fail(error);
            }
        }];
    
}

+ (void)requestGoodsTypeLowerWithCategoryId:(NSString *)cateId Success:(void(^)(NSArray<YGGoodsSelectModel *> *model))success fail:(void (^)(NSError *error))fail{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:cateId forKey:@"parentId"];
    [[CNHTTPManager shared] GET:YG_GOODS_TYPE_LOWER_URL parameters:dic success:^(id responeObj) {
        NSArray *data = [responeObj objectForKey:@"data"];
        NSArray *models = [YGGoodsSelectModel arrayOfModelsFromDictionaries:data];
            if (success) {
                success(models);
            }
        } failure:^(NSError *error, NSString *errorDes) {
            if (fail) {
                fail(error);
            }
        }];
}

+ (void)requestProductWithTypeId:(NSString *)typeId
                    departmentId:(NSString *)departmentId
                            sort:(NSInteger)sort
                            page:(NSInteger)page
                            rows:(NSInteger)rows
                       goodsName:(NSString *)goodsName
                        minPrice:(CGFloat)minPrice
                        maxPrice:(CGFloat)maxPrice
                        Success:(void (^)(NSArray<YGProductItemModel *> *model,NSInteger totalPage))success fail:(void (^)(NSError *error))fail{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//    [dic fa_setObjectSafe:<#(id)#> forKey:<#(id<NSCopying>)#>]
    [dic fa_setObjectSafe:typeId forKey:@"typeId"];
    [dic fa_setObjectSafe:departmentId forKey:@"departmentId"];
    [dic fa_setObjectSafe:@(sort) forKey:@"sort"];
    [dic fa_setObjectSafe:@(page) forKey:@"page"];
    [dic fa_setObjectSafe:@(20) forKey:@"rows"];
    [dic fa_setObjectSafe:goodsName forKey:@"goodsName"];
    if (minPrice != 0) {
        [dic fa_setObjectSafe:@(minPrice) forKey:@"minPrice"];
    }
    if (maxPrice != 0) {
        [dic fa_setObjectSafe:@(maxPrice) forKey:@"maxPrice"];
    }
    [[CNHTTPManager shared] GET:YG_GOODS_LIST_URL parameters:dic success:^(id responeObj) {
        NSDictionary *data = [responeObj objectForKey:@"data"];
        NSArray *pageData = [data objectForKey:@"pageData"];
        NSInteger totalPage = [[data objectForKey:@"totalPage"] integerValue];
        NSArray *models = [YGProductItemModel arrayOfModelsFromDictionaries:pageData];
        if (success) {
            success(models,totalPage);
            }
        } failure:^(NSError *error, NSString *errorDes) {
            if (fail) {
                fail(error);
            }
        }];
}

+ (void)requestKeshiWithSuccess:(void (^)(NSArray<YGGoodsSelectModel *> *model))success fail:(void (^)(NSError *error))fail{
    [[CNHTTPManager shared] GET:YG_KESHI_URL parameters:nil success:^(id responeObj) {
            NSArray *data = [responeObj objectForKey:@"data"];
        NSArray <YGGoodsSelectModel *> *models = [YGGoodsSelectModel arrayOfModelsFromDictionaries:data];
            if (success) {
                success(models);
            }
        } failure:^(NSError *error, NSString *errorDes) {
            if (fail) {
                fail(error);
            }
        }];
}
@end
